package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

import core.Controler;

public class Window extends JFrame {
	
	private JButton buttonStop;
	private JButton buttonLaunch;
	private JButton buttonReset;
	private JTextField textField;
	
	private Controler controler;
	
	public Window() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
//		setSize(300, 300);		
		
		addButton();
		addTextField();
		
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}
	
	public void setControler(Controler controler) {
		this.controler = controler;
	}
	
	public void setTextField(String text) {
		textField.setText("" + text);
	}
	
	private void addTextField() {
		textField = new JTextField();
		textField.setEditable(false);
		getContentPane().add(textField, BorderLayout.NORTH);
	}
	
	private void addButton() {
		JPanel panelSOUTH = new JPanel();
		panelSOUTH.setLayout(new BorderLayout());
		buttonStop = new JButton("STOP");	
		buttonStop.setEnabled(false);
		panelSOUTH.add(buttonStop, BorderLayout.WEST);
		buttonStop.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("STOP");
				if (controler != null) controler.killclock();
				buttonReset.setEnabled(true);
				buttonStop.setEnabled(false);
			}
		});
		
		buttonLaunch = new JButton("CREATE CLOCK");
		buttonLaunch.setEnabled(true);
		panelSOUTH.add(buttonLaunch, BorderLayout.SOUTH);
		buttonLaunch.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("CREATE CLOCK");
				if (controler != null) controler.runClock();
				buttonLaunch.setEnabled(false);
				buttonStop.setEnabled(true);
			}
		});
		
		buttonReset = new JButton("RESET");
		buttonReset.setEnabled(false);
		panelSOUTH.add(buttonReset, BorderLayout.EAST);
		buttonReset.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("RESET");
				textField.setText("");
				buttonReset.setEnabled(false);
				buttonLaunch.setEnabled(true);
			}
		});
		
		getContentPane().add(panelSOUTH, BorderLayout.CENTER);
	}
	
	public static void main(String[] args) {
		new Window();
	}
}
